<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//平台投资记录表
class Plat_invest_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->plat_invest_table = 'plat_invest_list';
        $this->plat_table = "plat";
        $this->plat_ac_cpa_table = "plat_ac_cpa";
        $this->users_table = 'users';
        $this->plat_invest_line_table = 'plat_invest_list_line';
    }
    
    function get_plat_invest($where){
        $this->db->select($this->plat_invest_table.'.*');
        $this->db->from($this->plat_invest_table);
        $this->db->where($where);
        $this->db->order_by("create_time asc");
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function get_invest_list($where,$page,$limit){
        $this->db->select($this->plat_invest_table.'.*,'.$this->users_table.".username,".$this->plat_table.".name_ch,".$this->plat_table.".name_en,".$this->plat_table.".plat_type,".$this->plat_table.".plat_period");
        $this->db->from($this->plat_invest_table);
        $this->db->join($this->users_table,$this->users_table.'.user_id = '.$this->plat_invest_table.'.user_id');
        $this->db->join($this->plat_table,$this->plat_table.'.id = '.$this->plat_invest_table.'.plat_id');
        $this->db->where($where);
        $this->db->order_by('create_time desc,invest_time desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function get_invest_count($where){
        $this->db->select('*');
        $this->db->from($this->plat_invest_table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->num_rows();
    }
    function get_invest_total_amount($where){
        $this->db->select('SUM(amount) amount');
        $this->db->from($this->plat_invest_table);
        $this->db->where($where);
        $query = $this->db->get()->row_array();
        return $query['amount'];
    }
    //插入数据
    function insert($data)
    {
            $this->db->insert($this->plat_invest_table, $data);
            if($this->db->affected_rows()){
               return $this->db->insert_id();
            }else{
                return 0;
            }
    }
    
    
    function get_invest_line_count($where){
        $this->db->select('*');
        $this->db->from($this->plat_invest_line_table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function get_invest_line_list($where,$page,$limit){
        $this->db->select($this->plat_invest_line_table.'.*');
        $this->db->from($this->plat_invest_line_table);
        $this->db->where($where);
        $this->db->order_by('invest_time desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    function get_invest_line_total_amount($where){
        $this->db->select('SUM(amount) amount');
        $this->db->from($this->plat_invest_line_table);
        $this->db->where($where);
        $query = $this->db->get()->row_array();
        return $query['amount'];
    }
    function insert_batch($datas)
    {
            $this->db->insert_batch($this->plat_invest_line_table, $datas);
    }
    
    //更新数据
    function update($data,$id)
    {
            return $this->db->where('id',$id)->update($this->table, $data);
    }
    
    //删除数据
    function dalete($delid)
    {	
            $this->db->where('id',$delid);
            $this->db->delete($this->table);
            return $this->db->affected_rows();
    }
    
    //设置状态
    function setstatus($status,$ids)
    {
            return $this->db->where_in('id',$ids)->update($this->table, array('status'=>$status));
    }
}
